'use strict';

 function btnFind(){
    let keyword = $('[name=keyword]').val();
    let tr = $('.id1');
    let tbData = $('#tbData');
    keyword = !keyword ? "" : keyword;
    $.ajax({
        url:`/student/find?keyword=${keyword}`,
        type:'get',
        success:(res) => {
         tr.remove();
         res.data.forEach(item => {
            console.log(item);
            let html = `
            <tr class="id1">
            <td>${item.id}</td>
            <td>${item.studentName}</td>
            <td>${item.birth}</td>
            <td>${item.gender}</td>
            <td>
                <input type="button" value="编辑">
                <input type="button" value="删除">
            </td>
        </tr>
        `;
        tbData.append(html);
         });
        },
        error:(err) => {
            console.log(err);
        }
    })
 }

 $(function () {
    let edit = $('[value=编辑]');
    let del = $('[value=删除]');

    edit.click(btnEdit);
    del.click(btnDel);
})

function btnDel(e){
    let res = confirm('你确定要删除吗？');
     if(res){
        let currentTarget = e.currentTarget;
        let tr = $(currentTarget).parent().parent();
        let id = tr.find('td').first().text();
        $.ajax({
            url:'/student/del/'+id,
            type:'delete',
            success:(res) => {
                console.log(res);
                tr.remove();
            },
            error:(err) =>{
                console.log(err);
            }
        })
     }
}

function btnAdd() {
    let id = $('tr[class=id1]:first').find('td').first().text();
    var ADD = confirm('默认更改第一个学生的信息，如果您需添加学生，请核对好学生ID');
    if(ADD){
    location.href = '/student/edit/'+ id;
    }
}
function btnEdit(e) {
    let currentTarget = e.currentTarget;
    let tr = $(currentTarget).parent().parent();
    let id = tr.find('td').first().text();
    location.href = '/student/edit/' + id;
}